package com.ktgj.hotel.controller;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.enums.EmHelper;
import com.ktgj.common.core.web.page.TableDataInfo;
import com.ktgj.common.log.annotation.SignatureValidation;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.BankCard;
import com.ktgj.customer.api.domain.Employee;
import com.ktgj.hotel.common.ElongUtils;
import com.ktgj.hotel.domain.HotelCashier;
import com.ktgj.hotel.domain.HotelOrder;
import com.ktgj.hotel.domain.HotelOrderException;
import com.ktgj.hotel.domain.HotelOrderPaydetail;
import com.ktgj.hotel.domain.base.*;
import com.ktgj.hotel.domain.common.Hotels;
import com.ktgj.hotel.domain.elong.*;
import com.ktgj.hotel.domain.rooms.*;
import com.ktgj.hotel.domain.rooms.HotelDetails;
import com.ktgj.hotel.domain.vo.*;
import com.ktgj.hotel.mapper.HotelOrderExceptionMapper;
import com.ktgj.hotel.mapper.HotelOrderMapper;
import com.ktgj.hotel.service.IHotelOrderPaydetailService;
import com.ktgj.hotel.service.IHotelSupplierLinkService;
import com.ktgj.hotel.utils.IsMobile;
import com.ktgj.hotel.utils.jdbc.JdbcUtil;
import com.ktgj.pay.api.RemotePayService;
import com.ktgj.pay.api.domain.BankCardApi;
import com.ktgj.pay.api.domain.BankPayInfoApi;
import com.ktgj.pay.api.domain.CustomerApi;
import com.ktgj.pay.api.domain.YeeBaoResultApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.hotel.service.IHotelService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import sun.util.locale.StringTokenIterator;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;

/**
 * 酒店Controller
 * 
 * @author ruoyi
 * @date 2021-11-22
 */
@ControllerAdvice
@RestController
@RequestMapping("/hotel")
public class HotelController extends BaseController {

    @Autowired
    private IHotelService hotelService;
    @Autowired
    private RemoteCustomerService remoteCustomerService;
    @Autowired
    private RemotePayService remotePayService;
    @Autowired
    private IHotelOrderPaydetailService hotelOrderPaydetailService;
    @Autowired
    private IHotelSupplierLinkService hotelSupplierLinkService;
    @Autowired
    private HotelOrderExceptionMapper hotelOrderExceptionMapper;


//    /**
//     * 查询酒店列表
//     */
//    @GetMapping("/list")
//    public TableDataInfo list(Hotels hotel) {
//        startPage();
//        List<Hotels> list = hotelService.selectHotelList(hotel);
//        return getDataTable(list);
//    }
//
//    /**
//     * 导出酒店列表
//     */
//    @Log(title = "酒店", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Hotels hotel) throws IOException {
//        List<Hotels> list = hotelService.selectHotelList(hotel);
//        ExcelUtil<Hotels> util = new ExcelUtil<Hotels>(Hotels.class);
//        util.exportExcel(response, list, "酒店数据");
//    }
//
//    /**
//     * 获取酒店详细信息
//     */
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id) {
//        return AjaxResult.success(hotelService.selectHotelById(id));
//    }
//
//    /**
//     * 新增酒店
//     */
//    @Log(title = "酒店", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody Hotels hotel) {
//        return toAjax(hotelService.insertHotel(hotel));
//    }
//
//    /**
//     * 修改酒店
//     */
//    @Log(title = "酒店", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody Hotels hotel) {
//        return toAjax(hotelService.updateHotel(hotel));
//    }
//
//    /**
//     * 删除酒店
//     */
//    @Log(title = "酒店", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids) {
//        return toAjax(hotelService.deleteHotelByIds(ids));
//    }


    /**
     * 根据条件查询酒店列表
     * @param hotelInput
     * @return
     */
    @PostMapping("/queryHotelList")
    public AjaxResult queryHotelList(@RequestBody HotelInput hotelInput) {
        List<HotelOutput> hotelOutputs = hotelService.selectHotelLists(hotelInput);
        if (hotelOutputs != null) {
            return AjaxResult.success(hotelOutputs);
        } else {
            return AjaxResult.error("查询酒店超时,请重试");
        }
    }

    /**
     * 查询酒店所有信息
     * @param input
     * @return
     */
    @PostMapping("/queryByIdHotel")
    public AjaxResult queryByIdHotel(@RequestBody HotelDetailInput input) {
        HotelDetailOutput hotelDetailOutput;
        if (input.HotelIds != null && !input.HotelIds.equals("") &&
                input.ArrivalDate != null && !input.ArrivalDate.equals("") &&
                input.DepartureDate != null && !input.DepartureDate.equals("") &&
                input.RoomTypeId != null && !input.RoomTypeId.equals("")){
            hotelDetailOutput = ElongUtils.queryByIdHotel(input);
            if (!hotelDetailOutput.Code.equals("0")){
                return AjaxResult.error("未查询到酒店信息");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }

        if (hotelDetailOutput.Code.equals("0")) {
            return AjaxResult.success(hotelDetailOutput);
        } else {
            return AjaxResult.error("信息查询失败");
        }
    }

    /**
     * 查询酒店房型信息
     * @param input
     * @return
     */
    @PostMapping("/queryByIdHotelDetail")
    public AjaxResult queryByIdHotelDetail(@RequestBody HotelDetailInput input) {
        HotelDetails hotelDetails;
        try {
            hotelDetails = hotelService.queryByIdHotelDetail(input);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (hotelDetails.HotelId != null) {
            return AjaxResult.success(hotelDetails);
        } else {
            return AjaxResult.error("信息获取失败");
        }
    }

    /**
     * 查询酒店房型信息详情
     * @param input
     * @return
     */
    @PostMapping("/queryByIdHotelDetailsList")
    public AjaxResult queryByIdHotelDetailsList(@RequestBody HotelDetailInput input) {
        HotelDetailsList hotelDetailsList;
        try {
            hotelDetailsList = hotelService.queryByIdHotelDetailsList(input);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (hotelDetailsList.HotelId != null) {
            return AjaxResult.success(hotelDetailsList);
        } else {
            return AjaxResult.error("信息获取失败");
        }
    }

    /**
     * 酒店房型页图片集合
     * @param input
     * @return
     */
    @PostMapping("/hotelRoomImages")
    public AjaxResult hotelRoomImages(@RequestBody HotelDetailInput input) {
        HotelRoomImagesResponse response;
        try {
            response = hotelService.hotelRoomImages(input);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (response.getImageLists().size() > 0) {
            return AjaxResult.success(response);
        } else {
            return AjaxResult.error("该酒店图片未查询到,请重试");
        }
    }

    /**
     * 酒店房型页酒店信息
     * @param input
     * @return
     */
    @PostMapping("/hotelRoomInfo")
    public AjaxResult hotelRoomInfo(@RequestBody HotelDetailInput input) {
        HotelRoomInfoResponse response;
        try {
            response = hotelService.hotelRoomInfo(input);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (response.getHotelInfos().getHotelName() != null) {
            return AjaxResult.success(response);
        } else {
            return AjaxResult.error("查询信息失败");
        }
    }

    /**
     * 订单预定页-预定须知
     * @param input
     * @return
     */
    @PostMapping("/queryBooking")
    public AjaxResult queryBooking(@RequestBody HotelDetailInput input) {
        BookingInfo bookingInfo;
        try {
            bookingInfo = hotelService.queryBooking(input);
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

        if (bookingInfo.getGeneralAmenities() != null) {
            return AjaxResult.success(bookingInfo);
        } else {
            return AjaxResult.success("当前酒店暂无预定须知");
        }
    }

    /**
     * 填写订单
     * @param request
     * @param map
     * @return
     */
    //@SignatureValidation
    @PostMapping("/orderHotel")
    public AjaxResult orderHotel(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        IsMobile isMobile = new IsMobile();

        String token;
        try {
            token = request.getHeader("token");
        } catch (Exception e){
            return AjaxResult.expired("登录已过期，请重新登录");
        }

        OrderHotelInput input = new OrderHotelInput();
        if (map.get("ArrivalDate") != null && !map.get("ArrivalDate").equals("") &&
                map.get("DepartureDate") != null && !map.get("DepartureDate").equals("") &&
                map.get("HotelIds") != null && !map.get("HotelIds").equals("") &&
                map.get("RoomTypeId") != null && !map.get("RoomTypeId").equals("") &&
                map.get("RatePlanId") != null && !map.get("RatePlanId").equals("")){
            input.ArrivalDate = map.get("ArrivalDate").toString();//入住日期
            input.DepartureDate = map.get("DepartureDate").toString();//离店日期
            input.HotelIds = map.get("HotelIds").toString();//酒店ID列表
            input.RoomTypeId = map.get("RoomTypeId").toString();//产品id
            input.RatePlanId = Long.valueOf(map.get("RatePlanId").toString());
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }

        if (map.get("ToTime") != null && !map.get("ToTime").equals("")){
            input.toTime = map.get("ToTime").toString();//预计到店时间
        }
        if (map.get("RoomNum") != null && !map.get("RoomNum").equals("") && !map.get("RoomNum").equals("0")){
            input.roomNum = Long.valueOf(map.get("RoomNum").toString());//预定房间数量
        }
        if (map.get("Price") != null && !map.get("Price").equals("") &&
                !map.get("Price").equals("0") && !map.get("Price").equals("0.0")){
            input.price = new BigDecimal(map.get("Price").toString());//价格
        }
        if (map.get("Phone") != null && !map.get("Phone").equals("")){
            String phone = map.get("Phone").toString();
            boolean valid = isMobile.checkPhoneValid(phone);
            if (phone.length() == 11 && valid){
                input.phone = phone;//联系方式
            }
            else {
                return AjaxResult.error("手机号格式不正确");
            }
        }
        if (map.get("PassengerLists") != null && !map.get("PassengerLists").equals("")){
            input.passengerLists = (List<PassengerLists>) map.get("PassengerLists");//入住人
        }

        OrderHotelOutput orderHotelOutput = hotelService.orderHotel(token, input);
        if (orderHotelOutput.hotelLists.HotelId != null) {
            return AjaxResult.success(orderHotelOutput);
        } else {
            return AjaxResult.error("创建订单失败");
        }
    }

    /**
     * 国内酒店-汇率
     * @param condition
     * @return
     */
    @GetMapping("/exchangeRate")
    public AjaxResult exchangeRate(ExchangeRateCondition condition) {
        return AjaxResult.success(ElongUtils.exchangeRate(condition));
    }

    /**
     * 国内酒店-信用卡验证
     * @param condition
     * @return
     */
    @GetMapping("/validateCreditCard")
    public AjaxResult validateCreditCard(@RequestBody ValidateCreditCardCondition condition) {
        return AjaxResult.success(ElongUtils.validateCreditCard(condition));
    }

    /**
     * 国内酒店-订单催确认
     * @param condition
     * @return
     */
    @GetMapping("/orderPromote")
    public AjaxResult orderPromote(OrderPromoteCondition condition) {
        return AjaxResult.success(ElongUtils.orderPromote(condition));
    }

    /**
     * 国内酒店-下单前数据验证
     * @param condition
     * @return
     */
    @GetMapping("/hotelValidate")
    public AjaxResult hotelValidate(ValidateCondition condition) {
        return AjaxResult.success(ElongUtils.hotelValidate(condition));
    }

    /**
     * 国内酒店-预定数据
     * @param condition
     * @return
     */
    @GetMapping("/hotelBooking")
    public AjaxResult hotelBooking(BookingDataCondition condition) {
        return AjaxResult.success(ElongUtils.hotelBooking(condition));
    }

    /**
     * 酒单创建订单（数据库）
     * */
    //@SignatureValidation
    @PostMapping("/createOrder")
    public AjaxResult order(HttpServletRequest request, @RequestBody CreateHotelOrder condition){
        String token = null;
        Employee employee = null;
        if (condition.customerId == null || condition.customerId == 0){
            try {
                token = request.getHeader("token");
            } catch (Exception e){
                return AjaxResult.expired("登录已过期，请重新登录");
            }
        }
        else {
            employee = remoteCustomerService.getLoginEmployeeByToken(token).getData();
            if (employee != null){
                return AjaxResult.expired("登录已过期，请重新登录");
            }
        }

        HotelDetailInput hotelDetailInput = new HotelDetailInput();
        hotelDetailInput.ArrivalDate = condition.ArrivalDate;
        hotelDetailInput.DepartureDate = condition.DepartureDate;
        hotelDetailInput.HotelIds = condition.HotelId;
        hotelDetailInput.RoomTypeId = condition.RoomTypeId;
        hotelDetailInput.Options = condition.Options;
        HotelDetailOutput hotelDetailOutput = null;
        if (condition.AffiliateConfirmationId == null) {
            try {
                hotelDetailOutput = ElongUtils.queryByIdHotel(hotelDetailInput);
            } catch (Exception e){
                return AjaxResult.error("未查到酒店信息");
            }
        }
        HotelIdNumVo order;
        try {
            order = hotelService.order(token, condition, condition.customerId, hotelDetailOutput);
        } catch (Exception e){
            e.printStackTrace();

            HotelOrderException hotelOrderException = new HotelOrderException();

            for (int i = 0; i < condition.Customers.size(); i++) {
                if (i == 0){
                    hotelOrderException.setPerson(condition.Customers.get(i).name);
                }
                else {
                    hotelOrderException.setPerson(hotelOrderException.getPerson() + "|" + condition.Customers.get(i).name);
                }
            }

            hotelOrderException.setPersonPhone(condition.Mobile);
            if (hotelDetailOutput != null)
                hotelOrderException.setHotelName(hotelDetailOutput.Result.getHotels().get(0).getDetail().get(0).getHotelName());
            else
                hotelOrderException.setHotelName(condition.HotelName);
            hotelOrderException.setReserveTime(new Date());
            hotelOrderException.setExceptionInfo(e.getMessage());
            hotelOrderException.setSourceWay(condition.hotelType);
            hotelOrderException.setStatus("untreated");

            if (employee == null){
                hotelOrderException.setEmployeeId(10l);
            }
            else {
                hotelOrderException.setEmployeeId(employee.getEmployeeId());
            }

//            String orderId = e.getMessage().substring(e.getMessage().length() - 10);
//
//            try {
//                Long id = Long.valueOf(orderId);
//                hotelOrderException.setOrderId(id.toString());
//            } catch (Exception e1){
//
//            }

            hotelOrderException.setOrderType("reserve");
            hotelOrderExceptionMapper.insertHotelOrderException(hotelOrderException);

            return AjaxResult.error(e.getMessage());
        }

        if (order != null) {
            return AjaxResult.success(order);
        } else {
            return AjaxResult.error("订单创建失败");
        }
    }

    /**
     * 酒店创建订单（艳东版）
     * */
    @PostMapping("/hotelSelOrder")
    public AjaxResult hotelSelOrder(HttpServletRequest request, @RequestBody CreateOrderVo condition){
        String token = null;
        if (condition.customerId == null || condition.customerId == 0){
            try {
                token = request.getHeader("token");
            } catch (Exception e){
                return AjaxResult.expired("登录已过期，请重新登录");
            }
        }

        HotelIdNumVo order;
        try {
            order = hotelService.hotelSelOrder(token, condition, condition.customerId);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (order != null) {
            return AjaxResult.success(order);
        } else {
            return AjaxResult.error("订单创建失败");
        }
    }

    /**
     * 国内酒店-订单支付确认
     * @param condition
     * @return
     */
    @PostMapping("/hotelOrderPayConfirm")
    public AjaxResult hotelOrderPayConfirm(OrderPayConfirmCondition condition) {
        return AjaxResult.success(ElongUtils.orderPayConfirm(condition));
    }

    /**
     * 国内酒店-订单支付
     * @param condition
     * @return
     */
    @PostMapping("/hotelOrderPay")
    public AjaxResult hotelOrderPay(@RequestBody OrderPayCondition condition) {
        OrderPayResult orderPayResult = ElongUtils.orderPay(condition);
        if (orderPayResult.Code.equals("0")) {
            return AjaxResult.success(orderPayResult);
        } else {
            return AjaxResult.error("支付请求未接收成功");
        }
    }

    /**
     * 酒店支付(小程序)
     * */
    @PostMapping("/hotelPays")
    public AjaxResult hotelPay(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        Long orderNumber = Long.valueOf(map.get("orderNumber").toString());
        String openId = map.get("openId").toString();

        BigDecimal amount = new BigDecimal(0);
        if (map.get("amount") != null && !map.get("amount").equals("")){
            amount = new BigDecimal(map.get("amount").toString());
        }

        String type = map.get("type").toString();

        Map<String, Object> wechatPay;
        try {
            wechatPay = hotelService.hotelPays(orderNumber, openId, request, amount, type);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (wechatPay != null)
            return AjaxResult.success(wechatPay);
        else
            return AjaxResult.error("支付失败");
    }

    /**
     * 酒店后端无卡无密/语音支付
     * */
    @PostMapping("/hotelPay")
    public AjaxResult hotelPay(HttpServletRequest request, @RequestBody HotelPayVo hotelPayVo){
        String hotelPay;
        try { hotelPay = hotelService.hotelPay(hotelPayVo, request);
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(hotelPay);
    }

    /**
     * 后台酒店付款
     * */
//    @PostMapping("/payment")
//    public AjaxResult payment(@RequestBody Map<String, Object> map){
//        if (map.get("jsjOrderId") != null && !map.get("jsjOrderId").equals("") &&
//                map.get("moneyAmout") != null && !map.get("moneyAmout").equals("") &&
//                map.get("custCertificateNo") != null && !map.get("custCertificateNo").equals("") &&
//                map.get("contractPhoneOne") != null && !map.get("contractPhoneOne").equals("") &&
//                map.get("cardNumber") != null && !map.get("cardNumber").equals("") &&
//                map.get("cardholder") != null && !map.get("cardholder").equals("") &&
//                map.get("creditCardTime") != null && !map.get("creditCardTime").equals("") &&
//                map.get("Cvv2") != null && !map.get("Cvv2").equals("") &&
//                map.get("bankName") != null && !map.get("bankName").equals("") &&
//                map.get("type") != null && !map.get("type").equals("")){
//
//
//        }
//        else {
//            return AjaxResult.error("缺少必要参数");
//        }
//    }

    /**
     * 酒店收银台
     * */
    @PostMapping("/payOrder")
    public AjaxResult payOrder(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        if (map.get("orderId") != null && !map.get("orderId").equals("")){
            String token = request.getHeader("token");
            Long orderId = Long.valueOf(map.get("orderId").toString());
            HotelCashier hotelCashier;
            try {
                hotelCashier = hotelService.payOrder(token, orderId, request);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

            if (hotelCashier != null) {
                return AjaxResult.success(hotelCashier);
            } else {
                return AjaxResult.error("查询错误");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 国内酒店-订单详情
     * @param condition
     * @return
     */
//    @SignatureValidation
    @PostMapping("/hotelOrderDetail")
    public AjaxResult hotelOrderDetail(@RequestBody OrderIdCondition condition) {
        //String token = request.getHeader("token");
        OrderDetailResult orderDetailResult = ElongUtils.orderDetail(condition);
        if (orderDetailResult.Code.equals("0")) {
            return AjaxResult.success(orderDetailResult);
        } else {
            return AjaxResult.error("订单详情查询失败,请稍后再试");
        }
    }

    /**
     * 国内酒店-订单列表
     * @param
     * @return
     */
//    @SignatureValidation
    @PostMapping("/hotelOrderList")
    public AjaxResult hotelOrderList(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        String type = null;
        if (map.get("type") != null && !map.get("type").equals("")){
            type = map.get("type").toString();
        }
        String token = request.getHeader("token");
        OrderList orderList;
        try {
            orderList = hotelService.orderList(token, type);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (orderList.getCount() != 0) {
            return AjaxResult.success(orderList);
        } else {
            return AjaxResult.error("查询错误或无订单");
        }
    }

    /**
     * 国内酒店-修改订单
     * @param condition
     * @return
     */
    @PostMapping("/updateOrder")
    public AjaxResult updateOrder(UpdateOrderCondition condition) {
        return AjaxResult.success(ElongUtils.updateOrder(condition));
    }

    /**
     * 国内酒店-删除订单
     * @param removeOrder
     * @return
     */
    @PostMapping("/deleteOrder")
    public AjaxResult deleteOrder(@RequestBody RemoveOrder removeOrder) {
        int i = hotelService.deleteOrder(removeOrder);
        if (i == 1) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 国内酒店-订单取消
     * @param condition
     * @return
     */
    @PostMapping("/hotelOrderCancel")
    public AjaxResult hotelOrderCancel(HttpServletRequest request, @RequestBody CancelOrderCondition condition) {
        CancelOrderResult cancelOrderResult;
        try {
            cancelOrderResult = hotelService.hotelOrderCancel(condition, request);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (cancelOrderResult.Code.equals("0")) {
            return AjaxResult.success(cancelOrderResult);
        } else {
            return AjaxResult.error("失败！",cancelOrderResult);
        }
    }

    /**
     * 国内酒店-补开发票
     * @param condition
     * @return
     */
    @GetMapping("/addInvoice")
    public AjaxResult addInvoice(AddInvoiceCondition condition) {
        return AjaxResult.success(ElongUtils.addInvoice(condition));
    }

    /**
     * 国内酒店-获取工单类别
     * @param condition
     * @return
     */
    @GetMapping("/srFindDic")
    public AjaxResult srFindDic(SrFindDicCondition condition) {
        return AjaxResult.success(ElongUtils.srFindDic(condition));
    }

    /**
     * 国内酒店-工单创建
     * @param condition
     * @return
     */
    @GetMapping("/srCreate")
    public AjaxResult srCreate(SrCreateCondition condition) {
        return AjaxResult.success(ElongUtils.srCreate(condition));
    }

    /**
     * 国内酒店-工单修改
     * @param condition
     * @return
     */
    @GetMapping("/srModify")
    public AjaxResult srModify(SrModifyCondition condition) {
        return AjaxResult.success(ElongUtils.srModify(condition));
    }

    /**
     * 国内酒店-工单催单
     * @param condition
     * @return
     */
    @GetMapping("/srUrge")
    public AjaxResult srUrge(SrUrgeCondition condition) {
        return AjaxResult.success(ElongUtils.srUrge(condition));
    }

    /**
     * 国内酒店-获取工单可视化信息
     * @param condition
     * @return
     */
    @GetMapping("/srVisual")
    public AjaxResult srVisual(SrVisualCondition condition) {
        return AjaxResult.success(ElongUtils.srVisual(condition));
    }

    /**
     * 条件查询酒店列表（艺龙）
     * */
    @PostMapping("/selectHotelsList")
    public AjaxResult dataStitching(@RequestBody HotelInput hotelInput) {
        //供应商（艺龙）枚举
        hotelInput.setHotelEnum("Elong");
        hotelInput.setLogo(0l);
        TotalVo totalVo;
        try {
            totalVo = hotelService.dataStitching(hotelInput);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (totalVo.getTotalNumber() > 0){
            if (totalVo.getHotelsVoList() != null && totalVo.getHotelsVoList().size() > 0){
                return AjaxResult.success(totalVo);
            }
            else {
                return AjaxResult.success("没有符合条件的酒店");
            }
        }
        else {
            return AjaxResult.error("未查到酒店信息");
        }
    }

    /**
     * 条件查询酒店列表(自营)
     * */
    @PostMapping("/selectHotels")
    public AjaxResult stitching(@RequestBody HotelInput hotelInput) {
        hotelInput.setHotelEnum("Manage");
        hotelInput.setLogo(0l);
        TotalVo totalVo;
        try {
            totalVo = hotelService.stitching(hotelInput);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (totalVo.getTotalNumber() > 0){
            if (totalVo.getHotelsVoList() != null && totalVo.getHotelsVoList().size() > 0){
                return AjaxResult.success(totalVo);
            }
            else {
                return AjaxResult.success("没有符合条件的酒店");
            }
        }
        else {
            return AjaxResult.error("未查到酒店信息");
        }
    }

    /**
     * 订单明细
     * */
    @PostMapping("/orderDetails")
    public AjaxResult orderDetails(@RequestBody Map<String, Object> map) throws Exception {
        Long orderid = null;
        Long orderNum = null;
        if (map.get("orderNum") != null && !map.get("orderNum").equals("")){
            orderNum = Long.valueOf(map.get("orderNum").toString());
        }
        else {
            orderid = Long.valueOf(map.get("orderid").toString());
        }
        Map<String, Object> hotelOrderDetails = hotelService.orderDetails(orderid, orderNum);
        if (hotelOrderDetails != null){
            return AjaxResult.success(hotelOrderDetails);
        }
        else {
            return AjaxResult.error("查询失败");
        }
    }

    /**
     * 查询酒店图片
     * */
    @PostMapping("/selectPicPath")
    public AjaxResult selectPicPath(@RequestBody OrderHotelInput input){
        if (input.HotelIds != null && !input.HotelIds.equals("") && !input.HotelIds.equals("0")){
            List<HotelPicPathVo> hotelPicPathVoList;
            try {
                hotelPicPathVoList = hotelService.selectPicPath(input);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

            if (hotelPicPathVoList != null && hotelPicPathVoList.size() > 0){
                return AjaxResult.success(hotelPicPathVoList);
            }
            else {
                return AjaxResult.error("未查到该酒店相关的任何图片信息");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 查询酒店图片
     * */
    @PostMapping("/selectPicPathVo")
    public AjaxResult selectPicPathVo(@RequestBody OrderHotelInput input){
        if (input.HotelIds != null && !input.HotelIds.equals("")){
            List<PicPathVo> picPathVoList;
            try {
                picPathVoList = hotelService.selectPicPathVo(input);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

            if (picPathVoList != null && picPathVoList.size() > 0){
                return AjaxResult.success(picPathVoList);
            }
            else {
                return AjaxResult.error("该酒店没有图片信息");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 酒店价格查询
     * */
    @PostMapping("/hotelPrice")
    public AjaxResult hotelPrice(@RequestBody HotelDetailInput input){
        if (input.HotelIds != null && !input.HotelIds.equals("") &&
                input.ArrivalDate != null && !input.ArrivalDate.equals("") &&
                input.DepartureDate != null && !input.DepartureDate.equals("") &&
                input.RoomTypeId != null && !input.RoomTypeId.equals("")){
            try {
                BigDecimal bigDecimal = hotelService.hotelPrice(input);
                return AjaxResult.success(bigDecimal);
            } catch (Exception e){
                return AjaxResult.error("查询异常");
            }

        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 酒店取消政策
     * */
    @PostMapping("/cancellationPolicy")
    public AjaxResult cancellationPolicy(@RequestBody HotelDetailInput input){
        if (input.HotelIds != null && !input.HotelIds.equals("") &&
                input.ArrivalDate != null && !input.ArrivalDate.equals("") &&
                input.DepartureDate != null && !input.DepartureDate.equals("") &&
                input.RoomTypeId != null && !input.RoomTypeId.equals("")){
            CancellationPolicy cancellationPolicy;
            try {
                cancellationPolicy = hotelService.cancellationPolicy(input);
                if (cancellationPolicy != null){
                    return AjaxResult.success(cancellationPolicy);
                }
                else {
                    return AjaxResult.error("未查到取消政策");
                }
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 特惠酒店
     * */
    @PostMapping("/specialHotel")
    public AjaxResult specialHotel(@RequestBody SpecialHotel specialHotel){
        if (specialHotel.getArrivalDate() != null && !specialHotel.getArrivalDate().equals("") &&
                specialHotel.getDepartureDate() != null && !specialHotel.getDepartureDate().equals("") &&
                specialHotel.getCityName() != null && !specialHotel.getCityName().equals("")){
            List<SpecialHotelVo> specialHotelVoList;
            try {
                specialHotelVoList = hotelService.specialHotel(specialHotel);
                return AjaxResult.success(specialHotelVoList);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 酒店房型筛选
     * */
    @PostMapping("/hotelRoomScreening")
    public AjaxResult hotelRoomScreening(@RequestBody RoomTypeVo roomTypeVo){
        if (roomTypeVo.getArrivalDate() != null && !roomTypeVo.getArrivalDate().equals("") &&
                roomTypeVo.getDepartureDate() != null && !roomTypeVo.getDepartureDate().equals("") &&
                roomTypeVo.getHotelId() != null && !roomTypeVo.getHotelId().equals("")){
            try {
                HotelDetails hotelDetails = hotelService.hotelRoomScreening(roomTypeVo);
                return AjaxResult.success(hotelDetails);
            } catch (Exception e){
                e.printStackTrace();
                return AjaxResult.error(e.getMessage());
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 热门酒店
     * */
    @PostMapping("/hotelPopular")
    public AjaxResult hotelPopular(){
        List<PopularHotel> popularHotelList;
        try {
            popularHotelList = hotelService.hotelPopular();
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (popularHotelList != null && popularHotelList.size() > 0){
            return AjaxResult.success(popularHotelList);
        }
        else {
            return AjaxResult.error("未查到相关数据");
        }
    }

    /**
     * 酒店取消订单
     * */
    @PostMapping("/cancelHotelOrder")
    public AjaxResult cancelHotelOrder(HttpServletRequest request, @RequestBody Map<String, Object> map){
        String token = request.getHeader("token");
        if (map.get("orderId") != null && !map.get("orderId").equals("")){
            Long orderId = Long.valueOf(map.get("orderId").toString());
            int i;
            try {
                i = hotelService.updateHotelOrder(token, orderId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (i > 0){
                return AjaxResult.success("取消成功");
            }
            else {
                return AjaxResult.error("取消失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 后台酒店条件查询
     * */
    @PostMapping("/hotelsList")
    public AjaxResult hotelsList(@RequestBody BackHotelVo backHotelVo){
        if (backHotelVo.getPages() == 0 || backHotelVo.getArticles() == 0){
            return AjaxResult.error("缺少必要参数");
        }

        List<HotelVo> hotelVos;
        try {
            hotelVos = hotelService.hotelsList(backHotelVo);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (hotelVos != null && hotelVos.size() > 0){
            return AjaxResult.success(hotelVos);
        }
        else {
            return AjaxResult.error("没有符合条件的数据");
        }
    }

    /**
     * 根据拼音查询酒店名称
     * */
    @PostMapping("/hotelVoByPinYin")
    public AjaxResult selectHotelVoByPinYin(@RequestBody Map<String, Object> map) {
        if (map.get("pinYin") != null && !map.get("pinYin").equals("")){
            String pinYin = map.get("pinYin").toString();

            String cityName = null;
            if (map.get("cityName") != null && !map.get("cityName").equals("")){
                cityName = map.get("cityName").toString();
            }

            List<HotelPinYinVo> hotelPinYinVoList;
            try {
                hotelPinYinVoList = hotelService.selectHotelVoByPinYin(pinYin, cityName);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

            if (hotelPinYinVoList != null && hotelPinYinVoList.size() > 0){
                return AjaxResult.success(hotelPinYinVoList);
            }
            else {
                return AjaxResult.error("未查到数据");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 后台查询酒店房型信息
     * */
    @PostMapping("/backByIdHotelDetail")
    public AjaxResult backByIdHotelDetail(@RequestBody HotelDetailInput input) {
        if (input != null){
            DetailsVo detailsVo;
            try {
                detailsVo = hotelService.backByIdHotelDetail(input);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

            return AjaxResult.success(detailsVo);
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 根据经纬度查询周边酒店
     * */
    @PostMapping("/selectHotelVoLngAndLat")
    public AjaxResult selectHotelVoLngAndLat(@RequestBody Map<String, Object> map) {
        if (map.get("lng") != null && !map.get("lng").equals("") && map.get("lat") != null && !map.get("lat").equals("")){
            String lng = map.get("lng").toString();
            String lat = map.get("lat").toString();

            List<ParameterHotelVo> parameterHotelVos;
            try {
                parameterHotelVos = hotelService.selectHotelVoLngAndLat(lng, lat);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

            if (parameterHotelVos != null && parameterHotelVos.size() > 0){
                return AjaxResult.success(parameterHotelVos);
            }
            else {
                return AjaxResult.error("周边没有酒店");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 查询酒店周边交通
     * */
    @PostMapping("/selectTrafficAround")
    public AjaxResult selectTrafficAround(@RequestBody Map<String, Object> map){
        if (map.get("lng") != null && !map.get("lng").equals("") &&
                map.get("lat") != null && !map.get("lat").equals("")){
            String lng = map.get("lng").toString();
            String lat = map.get("lat").toString();
            List<HotelTransportationVo> hotelTransportationVoList;
            try {
                hotelTransportationVoList = hotelService.selectTrafficAround(lng, lat);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

            return AjaxResult.success(hotelTransportationVoList);
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 查询酒店周边景点
     * */
    @PostMapping("/selectAttractions")
    public AjaxResult selectAttractions(@RequestBody Map<String, Object> map){
        if (map.get("lng") != null && !map.get("lng").equals("") &&
                map.get("lat") != null && !map.get("lat").equals("")){
            String lng = map.get("lng").toString();
            String lat = map.get("lat").toString();
            List<AttractionsVo> attractionsVos;
            try {
                attractionsVos = hotelService.selectAttractions(lng, lat);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

            return AjaxResult.success(attractionsVos);
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 酒店图片
     * */
    @PostMapping("/hotelPicture")
    public AjaxResult hotelPicture(@RequestBody OrderHotelInput input){
        if (input.HotelIds != null && !input.HotelIds.equals("") && !input.HotelIds.equals("0")){
            List<HotelPicPathVo> hotelPicPathVoList;
            try {
                hotelPicPathVoList = hotelService.hotelPicture(input);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

            if (hotelPicPathVoList != null && hotelPicPathVoList.size() > 0){
                return AjaxResult.success(hotelPicPathVoList);
            }
            else {
                return AjaxResult.error("未查到该酒店相关的任何图片信息");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 酒店订单详情
     * */
    @PostMapping("/selectHotelOrderDetails")
    public AjaxResult selectHotelOrderDetails(@RequestBody Map<String, Object> map){
        if (map.get("orderId") != null && !map.get("orderId").equals("")){
            Long orderId = Long.valueOf(map.get("orderId").toString());
            HotelOrderDetailsVo hotelOrderDetailsVo;
            try {
                hotelOrderDetailsVo = hotelService.selectHotelOrderDetails(orderId);
            } catch (Exception e){
                e.printStackTrace();
                return AjaxResult.error(e.getMessage());
            }

            return AjaxResult.success(hotelOrderDetailsVo);
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 条件查询酒店订单列表
     * */
    @PostMapping("/conditionHotelOrder")
    public AjaxResult conditionHotelOrder(@RequestBody HotelParameterVo hotelParameterVo){
        HotelOrderPaginationVo hotelOrderPaginationVo;
        try {
            hotelOrderPaginationVo = hotelService.conditionHotelOrder(hotelParameterVo);
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(hotelOrderPaginationVo);
    }

    /**
     * 条件查询酒店订单列表
     * */
    @PostMapping("/orderList")
    public HotelOrderPaginationVo orderList(@RequestBody HotelParameterVo hotelParameterVo){
        HotelOrderPaginationVo hotelOrderPaginationVo;
        try {
            hotelOrderPaginationVo = hotelService.conditionHotelOrder(hotelParameterVo);
        } catch (Exception e){
            return null;
        }
        return hotelOrderPaginationVo;
    }

    /**
     * 会员订单列表
     * */
    @PostMapping("/selectHotelOrderByCustomerId")
    public AjaxResult selectHotelOrderByCustomerId(@RequestBody Map<String, Object> map) {
        if (map.get("customerId") != null && !map.get("customerId").equals("")){
            Long customerId = Long.valueOf(map.get("customerId").toString());
            List<BackHotelOrderVo> backHotelOrderVoList;
            try {
                backHotelOrderVoList = hotelService.selectHotelOrderByCustomerId(customerId);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }
            if (backHotelOrderVoList != null && backHotelOrderVoList.size() > 0){
                return AjaxResult.success(backHotelOrderVoList);
            }
            else {
                return AjaxResult.error("没有数据");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 酒店支付宝支付
     * */
    @PostMapping("/aliPayCallback")
    public AjaxResult aliPayCallback(HttpServletRequest request, @RequestBody Map<String, Object> map){
        if (map.get("money") != null && !map.get("money").equals("") &&
                map.get("orderNumber") != null && !map.get("orderNumber").equals("")){
            BigDecimal money = new BigDecimal(map.get("money").toString());
            String orderNumber = map.get("orderNumber").toString();

            BigDecimal amount = new BigDecimal(0);
            if (map.get("amount") != null && !map.get("amount").equals("")){
                amount = new BigDecimal(map.get("amount").toString());
            }

            String type = map.get("type").toString();

            String hotelAliPayCallback;
            try {
                hotelAliPayCallback = hotelService.aliPayCallback(money, orderNumber, request, amount, type);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

            if (hotelAliPayCallback != null && !hotelAliPayCallback.equals("")){
                return AjaxResult.success(hotelAliPayCallback);
            }
            else {
                return AjaxResult.error("失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 酒店银联支付
     * */
    @PostMapping("/unionPayCallback")
    public AjaxResult unionPayCallback(HttpServletRequest request, @RequestBody Map<String, Object> map){
        if (map.get("orderIdParam") != null && !map.get("orderIdParam").equals("") &&
                map.get("priceParam") != null && !map.get("priceParam").equals("")){
            String orderIdParam = map.get("orderIdParam").toString();
            BigDecimal priceParam = new BigDecimal(map.get("priceParam").toString());

            BigDecimal amount = new BigDecimal(0);
            if (map.get("amount") != null && !map.get("amount").equals("")){
                amount = new BigDecimal(map.get("amount").toString());
            }

            String type = map.get("type").toString();

            String hotelUnionPayCallback;
            try {
                hotelUnionPayCallback = hotelService.unionPayCallback(orderIdParam, priceParam, request, amount, type);
            } catch (Exception e){
                return AjaxResult.error(e.getMessage());
            }

            if (hotelUnionPayCallback != null && !hotelUnionPayCallback.equals("")){
                return AjaxResult.success("成功", hotelUnionPayCallback);
            }
            else {
                return AjaxResult.error("500", "失败");
            }
        }
        else {
            return AjaxResult.error("缺少必要参数");
        }
    }

    /**
     * 测试用酒店创建订单
     * */
    @PostMapping("/bankCreateOrder")
    public AjaxResult bankCreateOrder(@RequestBody Map<String, Object> map){
        Long orderId = Long.valueOf(map.get("orderId").toString());
        String backNum = map.get("backNum").toString();
        CreateOrderResult createOrderResult;
        try {
            createOrderResult = hotelService.bankCreateOrder(orderId, backNum);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(createOrderResult);
    }

//    @PostMapping("/str")
//    public AjaxResult str(@RequestBody CancelOrderCondition condition){
//        CancelOrderResult cancelOrderResult = hotelService.cancelOrder(condition);
//        return AjaxResult.success(cancelOrderResult);
//    }

    @PostMapping("/str")
    public AjaxResult str(@RequestBody CancelOrderCondition condition){
        CancelOrderResult cancelOrderResult = hotelService.cancelOrder(condition);
        return AjaxResult.success(cancelOrderResult);
    }

    @PostMapping("/orderDetail")
    public AjaxResult orderDetail(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());

        HotelDetailOutput hotelDetailOutput;
        try {
            hotelDetailOutput = hotelService.orderDetails(id);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(hotelDetailOutput);
    }

    /**
     * 查询待填写订单
     * */
    @PostMapping("/selInterface")
    public AjaxResult selInterface(@RequestBody HotelDetailInput input){
        SelInterfaceVo selInterfaceVo;
        try {
            selInterfaceVo = hotelService.selInterface(input);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        return AjaxResult.success(selInterfaceVo);
    }

    /**
     * 推荐等级列表
     * */
    @PostMapping("/recommendLevel")
    public AjaxResult recommendLevel(){
        List<Long> hotels = hotelService.recommendLevel();
        return AjaxResult.success(hotels);
    }

    /**
     * 关键字不全
     * */
    @PostMapping("/destination")
    public AjaxResult destination(@RequestBody Map<String, Object> map){
        String name = map.get("name").toString();
        List<Destination> destinationList = hotelService.destination(name);
        return AjaxResult.success(destinationList);
    }

//    @PostMapping("/p")
//    public AjaxResult p(){
//        int count = 1000;
//        Long lastId = 1l;
//        List<Long> longList = new ArrayList<>();
//        HotelIncrDataResponse hotelIncrDataResponse;
//        do {
//            HotelIncrDataRequest request = new HotelIncrDataRequest();
//            request.setLastId(lastId);
//            request.setCount(count);
//            hotelIncrDataResponse = hotelSupplierLinkService.hotelIncrData(request);
//            if (hotelIncrDataResponse.getResult().getHotels() != null && hotelIncrDataResponse.getResult().getHotels().size() > 0){
//                for (HotelIncrDataResponse.HotelResult.Hotel hotel : hotelIncrDataResponse.getResult().getHotels()) {
//                    longList.add(hotel.getLastId());
//                }
//                longList.sort((x, y) -> Double.compare(y, x));
//                lastId = longList.get(0);
//            }
//        } while (hotelIncrDataResponse.getResult().getHotels() != null && hotelIncrDataResponse.getResult().getHotels().size() > 0);
//        return AjaxResult.success("ok");
//    }
}
